<template>
  <div class="activity-list">
    <van-tabs v-model="activeName" color="#1990FF">
      <van-tab v-for="(i, j) in tabArr" :key="j" :title="i" :name="j">
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            :finished-text="`${listData.length > 0 ? '没有更多了' : ''}`"
            @load="onLoad"
          >
            <template v-if="listData.length == 0 && !loading">
              <Nodata tips="当前无活动" />
            </template>
            <template v-for="(item, index) in listData">
              <activityCard :key="index" :item="item" />
            </template>
          </van-list>
        </van-pull-refresh>
      </van-tab>
    </van-tabs>
  </div>
</template>
<script>
import activityCard from '../../components/activityCard'
import Nodata from '../../components/noData'
export default {
  components: {
    activityCard,
    Nodata
  },
  data() {
    return {
      tabArr: ['最新活动', '往期活动', '我的活动'],
      activeName: 0,
      loading: false,
      finished: false,
      refreshing: false,
      listData: [],
      params: {
        page: 1,
        pageSize: 5
      }
    }
  },
  watch: {
    activeName(nval) {
      this.onRefresh(false)
      this.onLoad()
    }
  },
  created() {
    const q = this.$route.query
    console.log(q)
    this.activeName = +(q.index || 0)
  },
  methods: {
    // getData(index) {
    //   const arr = ['new', 'history', 'my']
    //   this.$axios({
    //     method: 'post',
    //     url: `/activity/list/${arr[index]}?page=${this.params.page}&pageSize=${this.params.pageSize}`
    //   }).then((res) => {
    //     this.listData = res.data.data.list
    //   })
    // },
    onLoad() {
      const arr = ['new', 'history', 'my']
      this.$axios({
        method: 'post',
        url: `/activity/list/${arr[this.activeName]}?page=${this.params.page}&pageSize=${this.params.pageSize}`
      }).then((res) => {
        if (this.refreshing) {
          this.listData = []
          this.refreshing = false
        }
        // 加载状态结束
        this.loading = false
        this.refreshing = false
        const d = (res && res.data && res.data.data) || {
          list: [],
          totalPage: 0
        }
        // 数据全部加载完成
        const { list = [] } = d
        this.listData.push(...list)
        if (d.totalPage <= this.params.page) {
          this.finished = true
        }
        this.params.page += 1
      })
    },
    onRefresh(flag = true) {
      this.params.page = 1
      // 清空列表数据
      this.finished = false
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.refreshing = true
      if (flag) {
        this.onLoad()
      }
    }
  }
}
</script>
<style scoped>
.van-tab--active .van-tab__text--ellipsis {
  font-size: 14px;
  font-family: PingFangSC-Medium;
  font-weight: 500;
  color: #323233;
}
.van-tab__text--ellipsis {
  font-size: 14px;
  font-weight: 400;
  color: #969799;
}
</style>
